package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebImagesDao;
import ysq.xyg.web.model.po.WebImages;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebImagesVo;
import ysq.xyg.web.service.WebImagesService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 网站模块-图片集service实现
 *
 * @author shengQiang yu
 * @date 2019-8-19 14:28:53
 */
@Service("webImagesService")
public class WebImagesServiceImpl extends BaseServiceImpl<WebImages> implements WebImagesService {

    @Resource
    private WebImagesDao webImagesDao;

    private WebImages webImages;

    @Autowired
    public void setBaseDao(WebImagesDao webImagesDao) {
        this.baseDao = webImagesDao;
    }


    /**
     * 分页查询
     *
     * @param webImagesVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-8-19 14:28:53
     */
    public ReturnEntity page(WebImagesVo webImagesVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<WebImages> list = listLeft(webImagesVo);
        PageInfo<WebImages> page=new PageInfo<WebImages>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webImagesVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-8-19 14:28:53
     */
    public ReturnEntity addOrUpdate(WebImagesVo webImagesVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webImagesVo.getId())){
            webImages=new WebImages(currentUser);
            isUpate=false;
        }else{
            webImages=selectByPrimaryKey(webImagesVo.getId());
            if(ObjectUtil.isNull(webImages)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webImages.setUpdateTime(new Date());
            webImages.setUpdateUser(currentUser.getId());
        }
        webImages.setName(webImagesVo.getName());
        webImages.setThumImage(webImagesVo.getThumImage());
        webImages.setImageContent(webImagesVo.getImageContent());
        webImages.setColId(webImagesVo.getColId());
        webImages.setSeoKeyword(webImagesVo.getSeoKeyword());
        webImages.setDescription(webImagesVo.getDescription());
        webImages.setSort(webImagesVo.getSort());
        webImages.setShowType(webImagesVo.getShowType());
        webImages.setUserId(webImagesVo.getUserId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webImages);
        }else{
            num=insert(webImages);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-8-19 14:28:53
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebImages webImages=selectByPrimaryKey(id);
        if(webImages!=null){
            webImages.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webImages.setUpdateTime(new Date());
            webImages.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webImages);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }
    /**
     * 关联获取集合
     *
     * @param webImagesVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-8-19 14:28:53
     */
    public List<WebImages> listLeft(WebImagesVo webImagesVo){
        return webImagesDao.listLeft(webImagesVo);
    }
    /**
     * 获取集合
     *
     * @param webImagesVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-8-19 14:28:53
     */
    public List<WebImages> acceptList(WebImagesVo webImagesVo){
        Example example = new Example(WebImages.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webImagesVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webImagesVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webImagesDao.selectByExample(example);
    }
}
